﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin002f : AdminBasePage
{
    decimal[] ids;
    decimal? thirdId;
    Guid? thirdCabinId;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["boxids"] != null)
        {
            ids = Request.QueryString["boxids"].TrimEnd(',').Split(',').Select(r => decimal.Parse(r)).ToArray();
        }
        if (!IsPostBack)
        {
            if (ids.Length > 0)
            {
                if (ids.Length == 1)
                {
                    FinBox finBox = new FinBox();
                    if (finBox.LoadByPrimaryKey(ids[0]))
                    {
                        thirdId = finBox.BoxLocation.Value;
                        thirdCabinId = finBox.ThirdUnitCabinId.Value;
                    }
                    btnSave.Text = "修改";
                }
                else
                {
                    FinBoxCollection boxs = new FinBoxCollection();
                    boxs.Query.Select(boxs.Query.BoxLocation, boxs.Query.ThirdUnitCabinId).Where(boxs.Query.BoxIndex.In(ids));
                    boxs.Query.es.Distinct = true;
                    boxs.Query.Load();
                    if (boxs.Count > 1)
                    {
                        btnSave.Visible = false;
                        lbMessage.Text = "选择的凭证盒不在同一个仓库，不能批量分配仓库号！";
                    }
                    else
                    { 
                        btnSave.Text = "批量修改";
                    }
                }


                if (thirdId.HasValue && thirdCabinId.HasValue)
                {
                    drpLocation.SelectedValue = thirdId.Value.ToString();
                    BFThirdUnitCabinCollection bFThirdUnitCabinCollection = new BFThirdUnitCabinCollection();
                    bFThirdUnitCabinCollection.Query.Where(bFThirdUnitCabinCollection.Query.ThirdUnitID == thirdId.Value);
                    drpThirdCabin.DataSource = bFThirdUnitCabinCollection.Query.LoadDataTable();
                    drpThirdCabin.DataTextField = BFThirdUnitCabinMetadata.ColumnNames.Name;
                    drpThirdCabin.DataValueField = BFThirdUnitCabinMetadata.ColumnNames.ThirdUnitCabinID;
                    drpThirdCabin.DataBind();
                    drpThirdCabin.SelectedValue = thirdCabinId.Value.ToString();

                    BFThirdUnitNumberCollection bFThirdUnitNumberCollection = new BFThirdUnitNumberCollection();
                    bFThirdUnitNumberCollection.Query.Where(bFThirdUnitNumberCollection.Query.ThirdUnitCabinID == thirdCabinId.Value);
                    drpThirdNumber.DataSource = bFThirdUnitNumberCollection.Query.LoadDataTable();
                    drpThirdNumber.DataTextField = BFThirdUnitNumberMetadata.ColumnNames.Name;
                    drpThirdNumber.DataValueField = BFThirdUnitNumberMetadata.ColumnNames.ThirdUnitNumberID;
                    drpThirdNumber.DataBind();
                    drpThirdNumber.Items.Insert(0, new ListItem("请选择凭证柜号", ""));
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
            FinBoxCollection boxs = new FinBoxCollection();
            boxs.Query.Where(boxs.Query.BoxID.In(ids));
            boxs.Query.Load();

           

            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (FinBox box in boxs)
                {
                    box.ThirdUnitNumberId=Guid.Parse(drpThirdNumber.SelectedValue);
                    box.StorageStatus = "S2";

                    FinReceiptCollection receipts = new FinReceiptCollection();
                    receipts.Query.Where(receipts.Query.BoxID == box.BoxID);
                    receipts.Query.Load();
                    foreach (FinReceipt receipt in receipts)
                    {
                        receipt.ThirdUnitNumberId = Guid.Parse(drpThirdNumber.SelectedValue);
                        receipt.StorageStatus = "S2";
                        receipt.FunctionDept = box.FunctionDept;
                        receipt.VoucherDescription = box.DocumentType;//凭证类型
                    }
                    receipts.Save();
                }

                boxs.Save();                
                tr.Complete();
            }

            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }

    }
}